package com.itheima.redbaby.activity;

/**
 * 促销快报
 */
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Topic;
import com.itheima.redbaby.engine.TopicEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.ImageUtil;
import com.itheima.redbaby.utils.PromptManager;

public class PromBulletinActivity extends BaseActivity {

	private TextView textTitle;
	private ListView promBulldtinLv;
	private MyPromBulletinAdapter adapter;
	private TextView backTv;

	// ======= 跟新ui 所需要的数据========
	private String cuxiao_title; // 活动主题
	private String src; // 活动图片
	// ====================

	private TopicEngine impl;
	private List<Topic> cuxiaoListData;
	private List<Topic> result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prom_bulletin_activity);
		textTitle = (TextView) findViewById(R.id.textTitle);
		backTv = (TextView) findViewById(R.id.backTv);
		textTitle.setText("促销快报");
		backTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent_home = new Intent(PromBulletinActivity.this,
						HomeActivity.class);
				startActivity(intent_home);
				finish();
			}
		});

		cuxiaoListData = new ArrayList<Topic>();
		promBulldtinLv = (ListView) findViewById(R.id.promBulldtinLv);
		impl = DaoAndEngineFactory.getImpl(TopicEngine.class);
		// impl.getTopicId();
		// 填充列表数据
		adapter = new MyPromBulletinAdapter();
		filldata();

	}

	public void filldata() {
		new MyHttpTask() {

			protected void onPreExecute() {
				PromptManager.showProgressDialog(PromBulletinActivity.this);
			};

			/***
			 * 获取后台数据
			 */
			@Override
			protected Object doInBackground(Void... params) {

				List<Topic> list = impl.getTopic();
				System.out.println(list.toString());
				return list;
			}

			/**
			 * 获取后台返回 的数据 并且跟新ui 获得的是 topic 对象
			 * 
			 * 更新 holder.textContent.setText("酷 ~宝贝  ~宝贝~ 裤" + "时间");
			 * holder.imgIcon.setImageResource(imgsid[position]);
			 */
			@Override
			protected void onPostExecute(Object result) {
				cuxiaoListData = (List<Topic>) result;

				promBulldtinLv.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				PromptManager.closeProgressDialog();
			}

		}.executeHttp();

		/**
		 * listview 中的跳转事件 点击时候我们 跳转到商品列表页面
		 */
		promBulldtinLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Topic topic = cuxiaoListData.get(position);
				String topicId = topic.getId();
				Intent intent = new Intent(PromBulletinActivity.this,
						CuXiaoProductListActivity.class);
				intent.putExtra("topicId", topicId);
				startActivity(intent);
			}
		});

	}

	// /////////////////////////
	/***
	 * 采用真实的数据 获得 图片的资源 获得 活动专题的id
	 * 
	 * 获得 topic 对象
	 * 
	 * @author xun
	 * 
	 * 
	 */

	class MyPromBulletinAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			if (cuxiaoListData == null)
				return 0;
			System.out.println(cuxiaoListData.size());
			return cuxiaoListData.size();

		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view;
			ViewHolder holder;
			if (convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(PromBulletinActivity.this,
						R.layout.prom_bulletin_item, null);
				holder = new ViewHolder();
				holder.textContent = (TextView) view
						.findViewById(R.id.textContent);
				holder.imgIcon = (ImageView) view.findViewById(R.id.imgIcon);
				view.setTag(holder);
			}
			Topic topic = cuxiaoListData.get(position);
			String src = topic.getSrc();
			String cuxiao_title = topic.getName();
			holder.textContent.setText(cuxiao_title + "2014-10-1--2014-11-11");
			ImageUtil.loadImageFromUrl(holder.imgIcon, src);

			return view;
		}
	}

	static class ViewHolder {
		TextView textContent;
		ImageView imgIcon;
	}

}
